package com.dailyblue.java.cloud.customer.feign.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.dailyblue.java.cloud.commons.util.JsonResult;
import com.dailyblue.java.cloud.commons.util.ResultTool;
import com.dailyblue.java.cloud.customer.feign.feign.ProviderFeign;
import com.dailyblue.java.cloud.customer.feign.handler.MyFallbackHandler;
import com.dailyblue.java.cloud.customer.feign.handler.MySecondBlockHandler;
import com.dailyblue.java.cloud.customer.feign.service.ProviderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ProviderServiceImpl implements ProviderService {
    @Resource
    private ProviderFeign providerFeign;

    @Override
    public JsonResult a() {
        return providerFeign.a();
    }

    @Override
    public JsonResult c() {
        return providerFeign.c();
    }

    @Override
    public JsonResult b() {
        return providerFeign.b();
    }

    @SentinelResource("psd")
    public JsonResult d() {
        return ResultTool.success("aaaaaaaaa");
    }

    @Override
    public JsonResult f() {
        return providerFeign.d();
    }

    @SentinelResource(value = "pse",
            blockHandler = "blockHandlerMethodE",blockHandlerClass = MySecondBlockHandler.class,
            fallback = "fallbackMethodE",fallbackClass = MyFallbackHandler.class)
    public JsonResult e() {
        int random = (int) (Math.random() * 3);
        return ResultTool.success("success!result:" + 8 / random);
    }
/*    public JsonResult blockHandlerMethodE(BlockException e) {
        return ResultTool.fail(500, "服务容错," + e.getMessage());
    }

    public JsonResult fallbackMethodE(Throwable e) {
        return ResultTool.fail(500, "代码出现了错误," + e.getMessage());
    }*/

}
